﻿namespace Trader.DataServices.FilesData
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text.RegularExpressions;
    using BO;
    using Interface;
    using Trader.BO.Interface;

    public class DataProvider : IDataProvider
    {
        #region Private members

        private readonly IFilesService filesService;

        #endregion

        #region const'

        public DataProvider(IFilesService filesService)
        {
            this.filesService = filesService;
        }

        public DataProvider()
        {
            filesService = new FilesService();
        }

        #endregion

        #region IDataProvider Members

        public IList<IStock> Stocks
        {
            get
            {
                var stocks = new List<IStock>();
                foreach (IFile file in filesService.GetStocksFiles())
                {
                    using (TextReader reader = file.GetReader())
                    {
                        string stockHeader = reader.ReadLine();
                        var quotesCollection = new QuotesCollection();
                        reader.ReadLine();
                        string quoteLine;
                        while ((quoteLine = reader.ReadLine()) != null)
                        {
                            if (
                                !quotesCollection.ContainsKey(DateTime.ParseExact(quoteLine.Split('\t')[0], "dd/MM/yyyy",
                                                                                  null)))
                            {
                                quotesCollection.Add(DateTime.ParseExact(quoteLine.Split('\t')[0], "dd/MM/yyyy", null),
                                                     new Quote(decimal.Parse(quoteLine.Split('\t')[3]),
                                                               decimal.Parse(quoteLine.Split('\t')[7]),
                                                               decimal.Parse(quoteLine.Split('\t')[8]),
                                                               decimal.Parse(quoteLine.Split('\t')[4])));
                            }
                        }

                        string stockName = GetNormalizedStockName(stockHeader);

                        stocks.Add(new Stock(int.Parse(stockHeader.Split('\t')[2]), stockName, quotesCollection));
                        reader.Close();
                    }
                }

                return stocks;
            }
        }

        public IList<ICurrecny> Currecnies
        {
            get { return null; }
        }

        #endregion

        private static string GetNormalizedStockName(string stockHeader)
        {
            string stockName = stockHeader.Split('\t')[0];
            var stockNameRegex = new Regex(@"[^\d]+");
            stockName = stockNameRegex.Match(stockName).Value;
            if (stockName.Contains(" מ\"ר"))
            {
                stockName = stockName.Remove(stockName.IndexOf(" מ\"ר"));
            }
            return stockName;
        }
    }
}